package com.atguigu.gulimall.product.config;

import com.atguigu.gulimall.product.config.properties.ThreadPoolProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

@Configuration
public class ThreadConfig {
    @Bean
    public ThreadPoolTaskExecutor taskExecutor(ThreadPoolProperties config) {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(config.getCorePoolSize());  // 设置核心线程池大小
        executor.setMaxPoolSize(config.getMaxPoolSize());   // 设置最大线程池大小
        executor.setKeepAliveSeconds(config.getKeepAliveSeconds());
        executor.setThreadNamePrefix("guli-thread-"); // 设置线程名称前缀
        // 其他配置属性，例如线程池关闭时等待任务完成等
        return executor;
    }
}
